;;; This file is part of guix-bavier.git
;;; Copyright © 2019 Eric Bavier <bavier@member.fsf.org>
;;; License: GPLv3+

(define-module (bavier packages admin)
  #:use-module (guix packages)
  #:use-module (guix git-download)
  #:use-module (guix build-system trivial)
  #:use-module (gnu packages)
  #:use-module (gnu packages bash)
  #:use-module (gnu packages ncurses)
  #:use-module ((guix licenses) #:prefix license:))

(define-public ufetch
  (let ((commit "877f675c990d087247630bd956e3ba8beae92654")
        (revision "1"))
    (package
     (name "ufetch")
     (version (git-version "0.1" revision commit))
     (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://gitlab.com/jschx/ufetch.git")
                    (commit commit)))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "0m7p2skwqq1z1zlc5qmg42wblha04jmj2hdby7blinzsv25ssmp8"))))
     (build-system trivial-build-system)
     (inputs
      `(("bash" ,bash)
        ("tput" ,ncurses)))
     (arguments
      `(#:modules ((guix build utils))
        #:builder (begin
                    (use-modules (guix build utils))
                    (let* ((source (assoc-ref %build-inputs "source"))
                           (output (assoc-ref %outputs "out"))
                           (bindir (string-append output "/bin"))
                           (docdir (string-append output "/share/doc/ufetch-" ,version))
                           (tput (string-append (assoc-ref %build-inputs "tput") "/bin/tput")))
                      (install-file (string-append source "/LICENSE") docdir)
                      (setenv "PATH" (string-append (assoc-ref %build-inputs "bash") "/bin"))
                      (mkdir-p bindir)
                      (for-each (lambda (src)
                                  (let ((dst (string-append bindir "/" (basename src))))
                                    (copy-file src dst)
                                    (patch-shebang dst)
                                    (substitute* dst (("tput") tput))))
                                (find-files source "ufetch-[[:alpha:]]*$"))
                      ;; Note: the `ufetch` we create below will only work if
                      ;; run under the Guix System.  I.e. a user trying to run
                      ;; `ufetch` on a foreign distro will not get great
                      ;; results.  The `screenfetch` program does actual
                      ;; runtime detection of the operating system, and would
                      ;; be a better choice in such a situation.
                      (symlink "ufetch-guix" (string-append bindir "/ufetch"))))))
     (home-page "https://gitlab.com/jschx/ufetch")
     (synopsis "Tiny system info")
     (description "This package provides a tiny system info utility.")
     (license license:isc))))
